package www.xpu.edu.app.job;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import www.xpu.edu.app.R;

public class Activity_job_1 extends AppCompatActivity {
    /*recruit招聘表
    num 招聘者电话,由Activity传入
    corname 公司名称
    type 类型
    address 工作地点
    jobname 职位名称
    dep 所在部门
    exp 工作经验
    wage 每月薪水的下限
    grad 学历要求
    requ 岗位职责
         */
    String num,corname,type,address,jobname,dep,exp,wage,grad,requ;
   /* String corname="";
    String type="";
    String address="";
    String jobname="";
    String dep="";
    String exp="";
    String wage="";
    String grad="";
    String requ="";*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_1);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        //切换按钮
        Button button1=(Button)findViewById(R.id.title_change);
        button1.setVisibility(View.INVISIBLE);
        //让此按钮不可见
        /*
        VISIBLE：设置控件可见
        INVISIBLE：设置控件不可见
        GONE：设置控件隐藏*/
       // button1.setVisibility(View.GONE);
        //返回按钮
        Button button2=(Button)findViewById(R.id.title_back);
        //发布按钮
        Button button3=(Button)findViewById(R.id.job1_button1);
        //公司名称
        final EditText edit1=(EditText)findViewById(R.id.job1_edit1);
        corname=edit1.getText().toString().trim();
        //工作地点
        final EditText edit2=(EditText)findViewById(R.id.job1_edit2);
        address=edit2.getText().toString().trim();
        //职位名称
        final EditText edit3=(EditText)findViewById(R.id.job1_edit3);
        jobname=edit3.getText().toString().trim();
        //所在部门
        final EditText edit4=(EditText)findViewById(R.id.job1_edit4);
        dep=edit4.getText().toString().trim();
        //岗位职责及要求
        final EditText edit5=(EditText)findViewById(R.id.job1_edit5);
        requ=edit5.getText().toString();
        //工作经验
        Spinner spinner1=(Spinner)findViewById(R.id.job1_spinner1);
        final String[] str1=new String[]{"点此选择工作经验","实习生","半年以下","1年","2年","3年及以上","其他"};
        ArrayAdapter<String> adapter1=new ArrayAdapter<String>(Activity_job_1.this,R.layout.support_simple_spinner_dropdown_item,str1);
        spinner1.setAdapter(adapter1);
        //薪水的上下限
        Spinner spinner2=(Spinner)findViewById(R.id.job1_spinner2);
        final String[] str2=new String[]{"选择工资","3k","4k","5k","6k","7k","8k","9k","10k","10k以上"};
        ArrayAdapter<String> adapter2=new ArrayAdapter<String >(Activity_job_1.this,R.layout.support_simple_spinner_dropdown_item,str2);
        spinner2.setAdapter(adapter2);
        Spinner spinner3=(Spinner)findViewById(R.id.job1_spinner3);
        //str3根据需求做修改
        final String[] str3=new String[]{"选择工资","3k","4k","5k","6k","7k","8k","9k","10k","10k以上"};
        ArrayAdapter<String> adapter3=new ArrayAdapter<String >(Activity_job_1.this,R.layout.support_simple_spinner_dropdown_item,str3);
        spinner3.setAdapter(adapter3);
        //学历
        Spinner spinner4=(Spinner)findViewById(R.id.job1_spinner4);
        final String[] str4=new String[]{"选择学历","专科及以下","大专高职","本科","985、211","研究生","博士生及以上"};
        ArrayAdapter<String> adapter4=new ArrayAdapter<String >(Activity_job_1.this,R.layout.support_simple_spinner_dropdown_item,str4);
        spinner4.setAdapter(adapter4);
        //招聘类型
       /* CheckBox check1=(CheckBox)findViewById(R.id.check1);
        CheckBox check2=(CheckBox)findViewById(R.id.check2);
        CheckBox check3=(CheckBox)findViewById(R.id.check3);*/
       //招聘类型-单选框
        RadioGroup group=(RadioGroup)findViewById(R.id.job1_group);



        //此字符串变量记录用户输入的所有数据，弹出吐司(测试阶段使用),后期将数据联网发送post请求写到服务器的数据库上
        String strs="";
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //销毁当前的Activity，达到“返回”的作用
                finish();
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Activity_job_1.this,"您点击了切换身份按钮，该功能预留中",Toast.LENGTH_LONG).show();
            }
        });


        //工作经验
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position!=0) {
                    Toast.makeText(Activity_job_1.this, "您点击了" + str1[position], Toast.LENGTH_SHORT).show();
                    exp = str1[position];
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position!=0)
                {Toast.makeText(Activity_job_1.this,"您点击了"+str2[position],Toast.LENGTH_SHORT).show();
                wage=str2[position];}
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position!=0)
                    Toast.makeText(Activity_job_1.this,"您点击了"+str3[position],Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        spinner4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position!=0)

                {
                    Toast.makeText(Activity_job_1.this, "您点击了" + str4[position], Toast.LENGTH_SHORT).show();
                    grad=str4[position];
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

      /*  check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked==true)
                    Toast.makeText(Activity_job_1.this,"全职",Toast.LENGTH_SHORT).show();
            }
        });
        check2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked==true)
                    Toast.makeText(Activity_job_1.this,"实习",Toast.LENGTH_SHORT).show();
            }
        });
        check3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked==true)
                    Toast.makeText(Activity_job_1.this,"兼职",Toast.LENGTH_SHORT).show();
            }
        });*/
      group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(RadioGroup group, int checkedId) {
              switch (checkedId) {
                  case (R.id.job1_rb1):
                      Toast.makeText(Activity_job_1.this, "全职", Toast.LENGTH_SHORT).show();
                      type="全职";
                      break;
                  case(R.id.job1_rb2):
                      Toast.makeText(Activity_job_1.this, "实习", Toast.LENGTH_SHORT).show();
                      type="实习";
                      break;
                  case(R.id.job1_rb3):
                      Toast.makeText(Activity_job_1.this, "兼职", Toast.LENGTH_SHORT).show();
                      type="兼职";
                      break;
                      default:
                          break;
              }
          }});

      button3.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              //startActivity(new Intent(Activity_job_1.this,Activity_job_2.class));
             //sendReguest(num,corname,type,address,jobname,dep,exp,wage,grad,requ);
              //Toast.makeText(Activity_job_1.this,"点击了按钮",Toast.LENGTH_LONG).show();
              //myapplication=(Myapplication)getApplication();
              sendReguest(/*myapplication.getNum()*/"19819981998",edit1.getText().toString(),type,edit2.getText().toString(),edit3.getText().toString(),edit4.getText().toString(),exp,wage,grad,edit5.getText().toString());
              //Toast.makeText(Activity_job_1.this,corname+"##"+jobname,Toast.LENGTH_LONG).show();

          }
      });

    }
    /*
    String num="";
    String corname="";
    String type="";
    String address="";
    String jobname="";
    String dep="";
    String exp="";
    String wage="";
    String grad="";
    String requ="";
     */
    public void sendReguest(final String num,final String corname,final String type,final String address,final String jobname,final String dep,final String exp,final String wage,final String grad,final String requ){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection=null;
                BufferedReader reader=null;
                //http://localhost:8080/xpu/login?num=19829088780&password=12345678
                try {
                    //188.131.250.4:8080
                    //URL url=new URL("http://192.168.123.196:8080/xpu/login"+"?num="+user+"&password="+password);
                    //URL url=new URL("http://188.131.250.4:8080/xpu/login"+"?num="+user+"&password="+password);
                    //临时测试的网址http://localhost:8080/test20190411_war_exploded/recruit1
                    //num实际过程中由intent传值得到（num值非常重要，故start每一个Activity的时候都需要传这个值）
                    //测试阶段,指定num为19819981998,华为公司的人力资源
                    URL url=new URL("http://188.131.250.4:8080/xpu/recruit1"+"?num="+num+"&corname="+corname+"&type="+type+"&address="+address+"&jobname="+jobname+"&dep="+dep+"&exp="+exp+"&wage="+wage+"&grad="+grad+"&requ="+requ);
                    connection=(HttpURLConnection)url.openConnection();
                    connection.setRequestMethod("GET");
                    //connection.setRequestMethod("POST");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    InputStream ips=connection.getInputStream();
                    reader=new BufferedReader(new InputStreamReader(ips));
                    StringBuilder response=new StringBuilder();
                    String line;
                    while((line=reader.readLine())!=null)
                    {
                        response.append(line);
                    }
                    /*if(response.toString().trim().equals("发布成功"))
                    {
                        Toast.makeText(Activity_job_1.this,"发布成功",Toast.LENGTH_LONG).show();
                    }*/
                   showResponse(response.toString().trim());


                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
   private void showResponse(final String response)
    {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(response.trim().equals("发布成功")) {
                    Toast.makeText(Activity_job_1.this,"发布成功", Toast.LENGTH_LONG).show();
                    //跳转到findjob
                    //startActivity(new Intent(Activity_job_1.this,Activity_findjob.class));
                }

                else
                    Toast.makeText(Activity_job_1.this,"发布失败",Toast.LENGTH_LONG).show();
            }
        });
    }
}
